package com.charonmc;

import com.charonmc.commands.BackCheckpoint;
import com.charonmc.commands.BackStart;
import com.charonmc.commands.End;
import com.charonmc.commands.SetPoint;
import com.charonmc.listeners.GeneralItemListener;
import com.charonmc.listeners.PlayerListener;
import com.charonmc.listeners.WorldListener;
import com.charonmc.parkour.PakourItemListener;
import com.charonmc.parkour.ParkourManager;
import com.charonmc.parkour.ParkourListener;
import com.charonmc.utils.MessageUtils;
import com.charonmc.utils.PrefixUtils;
import net.minecraft.server.v1_8_R3.IChatBaseComponent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;

import java.util.ArrayList;
import java.util.List;

public class CharonHub extends JavaPlugin {
    public static Location StartLocation = new Location(Bukkit.getWorld("world"), 0.0, 0, 0);
    public static Location EndLocation = new Location(Bukkit.getWorld("world"), 0, 0, 0);
    public static CharonHub instance;
    @Override
    public void onEnable(){
        instance = this;//////
        saveDefaultConfig();
        StartLocation.setWorld(Bukkit.getWorlds().get(0));
        StartLocation.setX(((Integer) getConfig().get("StartX")).doubleValue());
        StartLocation.setY(((Integer) getConfig().get("StartY")).doubleValue());
        StartLocation.setZ(((Integer) getConfig().get("StartZ")).doubleValue());
        EndLocation.setWorld(Bukkit.getWorlds().get(0));
        EndLocation.setX(((Integer) getConfig().get("EndX")).doubleValue());
        EndLocation.setY(((Integer) getConfig().get("EndY")).doubleValue());
        EndLocation.setZ(((Integer) getConfig().get("EndZ")).doubleValue());
        getLogger().info("[CharonHub]Start!!!!");
        Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);
        Bukkit.getPluginManager().registerEvents(new WorldListener(), this);
        Bukkit.getPluginManager().registerEvents(new ParkourListener(), this);
        Bukkit.getPluginManager().registerEvents(new PakourItemListener(), this);
        Bukkit.getPluginManager().registerEvents(new GeneralItemListener(), this);
        this.getCommand("BackCheckpoint").setExecutor(new BackCheckpoint());
        this.getCommand("SetPoint").setExecutor(new SetPoint());
        this.getCommand("End").setExecutor(new End());
        this.getCommand("BackStart").setExecutor(new BackStart());
        task.runTaskTimerAsynchronously(this, 0, 1);
        //registerTeam();
    }

    BukkitRunnable task = new BukkitRunnable() {
        @Override
        public void run() {
            for (Player player : ParkourManager.parkourPlayers.keySet()){
                Integer n = ParkourManager.parkourPlayers.get(player);
                n++;
                ParkourManager.parkourPlayers.put(player, n);
                if (n >= 72000){
                    ParkourManager.parkourPlayers.remove(player);
                    player.sendMessage("§c§l超时，跑酷已结束");
                } else {
                    IChatBaseComponent a = IChatBaseComponent.ChatSerializer.a(getTimeFormat(player));
                    MessageUtils.sendActionBar(player, a);
                }
            }
        }
    };

    public static String getTimeFormat(Player player){
        Integer time = ParkourManager.parkourPlayers.get(player);
        int min = time / 20 / 60;
        time -= min * 20 * 60;
        int sec = time / 20;
        time -= sec * 20;
        int msec = 1000 / 20 * time / 10;
        return "§c" + String.format("%02d", min) + "§b：§e" + String.format("%02d", sec) + "§b：§a" + String.format("%02d", msec);
    }

    public static int getParkourTimeTick(Player player){
        return ParkourManager.parkourPlayers.get(player);
    }

    public static String getFormat(int time){
        int min = time / 20 / 60;
        time -= min * 20 * 60;
        int sec = time / 20;
        time -= sec * 20;
        int msec = 1000 / 20 * time / 10;
        return "§c" + String.format("%02d", min) + "§b：§e" + String.format("%02d", sec) + "§b：§a" + String.format("%02d", msec);

    }
    public static void setPlayerDefaultInventory(Player player){
        player.getInventory().clear();
        ItemStack menu = new ItemStack(Material.COMPASS);
        ItemMeta menuMeta = menu.getItemMeta();
        menuMeta.setDisplayName("§e选择服务器 §7(右键打开)");
        menu.setItemMeta(menuMeta);

        ItemStack profile = new ItemStack(Material.SKULL_ITEM, 1, (short)3);
        SkullMeta skullMeta = (SkullMeta) profile.getItemMeta();
        skullMeta.setDisplayName("§e个人信息 §7(右键打开)");
        skullMeta.setOwner(player.getName());
        profile.setItemMeta(skullMeta);

        ItemStack shop = new ItemStack(Material.EMERALD);
        ItemMeta shopMeta = shop.getItemMeta();
        shopMeta.setDisplayName("§e游戏商店 §7(右键打开)");
        shop.setItemMeta(shopMeta);

        ItemStack select = new ItemStack(Material.NETHER_STAR);
        ItemMeta selectMeta = select.getItemMeta();
        selectMeta.setDisplayName("§e大厅选择器 §7(右键打开)");
        select.setItemMeta(selectMeta);

        ItemStack issue = new ItemStack(Material.BOOK);
        ItemMeta issueMeta = issue.getItemMeta();
        issueMeta.setDisplayName("§e提出建议 §7(右键打开)");
        List<String> temp = new ArrayList<>();
        temp.add("§e您的每个建议我们都会回复");
        temp.add("§e期待与您创造更好的世界");
        issueMeta.setLore(temp);
        issue.setItemMeta(issueMeta);

        ItemStack collection = new ItemStack(Material.CHEST);
        ItemMeta collectionMeta = collection.getItemMeta();
        collectionMeta.setDisplayName("§e收集箱 §7(右键打开)");
        collection.setItemMeta(collectionMeta);

        player.getInventory().setItem(0, menu);
        player.getInventory().setItem(1, profile);
        player.getInventory().setItem(4, collection);
        player.getInventory().setItem(6, issue);
        player.getInventory().setItem(7, shop);
        player.getInventory().setItem(8, select);
    }

    public static void registerTeam(){
        Scoreboard scoreboard = Bukkit.getServer().getScoreboardManager().getMainScoreboard();
        Team ownerTeam = scoreboard.registerNewTeam("0");
        ownerTeam.setPrefix("§8[§4服主§8]§4");
        Team adminTeam = scoreboard.registerNewTeam("1");
        adminTeam.setPrefix("§8[§d管理§8]§d");
        Team staffTeam = scoreboard.registerNewTeam("2");
        staffTeam.setPrefix("§8[§9监察§8]§9");
        Team tempStaffTeam = scoreboard.registerNewTeam("3");
        tempStaffTeam.setPrefix("§8[§3实习监察§8]§3");
        Team goldTeam = scoreboard.registerNewTeam("4");
        goldTeam.setPrefix("§8[§e金锭§8]§e");
        Team diamondTeam = scoreboard.registerNewTeam("5");
        diamondTeam.setPrefix("§8[§b钻石§8]§b");
        Team emeraldTeam = scoreboard.registerNewTeam("6");
        emeraldTeam.setPrefix("§8[§a绿宝石§8]§a");
        Team buildTeam = scoreboard.registerNewTeam("7");
        buildTeam.setPrefix("§8[§5设计§8]§5");
        Team codeTeam = scoreboard.registerNewTeam("8");
        codeTeam.setPrefix("§8[§c开发§8]§c");
        Team scriptTeam = scoreboard.registerNewTeam("9");
        scriptTeam.setPrefix("§8[§6策划§8]§6");
        Team normal = scoreboard.registerNewTeam("-1");
        normal.setPrefix("");
    }

    public static void addPlayerTeam(Player player){
        int target = PrefixUtils.getPlayerPrefixInt(player);
        Bukkit.getServer().getScoreboardManager().getMainScoreboard().getTeam(String.valueOf(target)).addEntry(player.getName());

    }
}